import 'package:flutter/cupertino.dart';

class CustomFlowDelegate extends FlowDelegate {
  EdgeInsets margin = EdgeInsets.zero;
  double maxWidth = 200;
  double height = 20;

  CustomFlowDelegate({required this.margin,required this.maxWidth, required this.height});
  @override
  void paintChildren(FlowPaintingContext context) {
    var x = margin.left;
    var y = margin.top;
    //计算每一个子widget的位置
    for (int i = 0; i < context.childCount; i++) {
      var w = context.getChildSize(i)!.width + x + margin.right;
      if (w < context.size.width) {
        context.paintChild(i,
            transform: Matrix4.translationValues(
                x, y, 0.0));
        x = w + margin.left;
      }
      // else {
      //   x = margin.left;
      //   y += context.getChildSize(i)!.height + margin.top + margin.bottom;
      //   //绘制子widget(有优化)
      //   context.paintChild(i,
      //       transform: new Matrix4.translationValues(
      //           x, y, 0.0));
      //   x += context.getChildSize(i)!.width + margin.left + margin.right;
      // }
    }
  }

  getSize(BoxConstraints constraints){
    //指定Flow的大小
    return Size(maxWidth,height);
  }

  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return oldDelegate != this;
  }
}